home *** CD-ROM | disk | FTP | other *** search
/ Aminet 5 / Aminet 5 - March 1995.iso / Aminet / dev / misc / LEDA_gene.lha / LEDA-3.1c-generic / incl / LEDA / impl / p_heap.h < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-05  |  3.0 KB  |  128 lines

  1. /*******************************************************************************
  2. +
  3. +  LEDA  3.1c
  4. +
  5. +
  6. +  p_heap.h
  7. +
  8. +
  9. +  Copyright (c) 1994  by  Max-Planck-Institut fuer Informatik
  10. +  Im Stadtwald, 6600 Saarbruecken, FRG     
  11. +  All rights reserved.
  12. *******************************************************************************/
  13.  
  14.  
  15. #ifndef LEDA_PAIRING_HEAP_H
  16. #define LEDA_PAIRING_HEAP_H
  17.  
  18. #include<LEDA/basic.h>
  19.  
  20. // Implementation following John T. Stasko and Jeffrey Scott Vitter
  21. // ( Communications of the ACM   March 1987 Volume 30 Number 3   S. 234-240 )
  22. //
  23. // by Markus Neukirch   (1992)
  24.  
  25. class ph_item
  26. {
  27.   friend class pairing_heap;
  28.   friend class p_heap;
  29.   friend class p_aux_heap;
  30.  
  31.         GenPtr key;
  32.         GenPtr inf;
  33.  
  34.         ph_item* parent;
  35.         ph_item* l_child;
  36.         ph_item* r_child;
  37.  
  38.  
  39.         ph_item(GenPtr k,GenPtr i)
  40.         { parent = l_child = r_child=nil;
  41.           key=k;
  42.           inf=i;
  43.          }
  44.  
  45.        ~ph_item()       {} 
  46.         
  47.   LEDA_MEMORY(ph_item)
  48.         
  49. };
  50.  
  51.  
  52. class p_heap
  53. {
  54.         
  55.         ph_item* head;
  56.         int item_count;
  57.         void do_copy(ph_item*,ph_item*,bool);
  58.         void copy_sub_tree(ph_item*, ph_item*);
  59.         void clear_sub_tree(ph_item*);
  60.         ph_item* new_ph_item(GenPtr,GenPtr);
  61.  
  62.         virtual int cmp(GenPtr x, GenPtr y) const {return compare (x,y);}
  63.  
  64.         virtual void print_key(GenPtr)  const {}
  65.         virtual void print_inf(GenPtr)  const {}
  66.         virtual void clear_key(GenPtr&)  const {}
  67.         virtual void clear_inf(GenPtr&)  const {}
  68.         virtual void copy_key(GenPtr&)   const {}
  69.         virtual void copy_inf(GenPtr&)   const {}
  70.  
  71.         virtual int  int_type() const { return 0; }
  72.  
  73. protected:
  74.  
  75.         //ph_item* comparison_link(ph_item*,ph_item*);
  76.  
  77.         ph_item* twopass(ph_item*);
  78.         ph_item* multipass(ph_item*);
  79.  
  80.         ph_item* item(void* p) const { return (ph_item*)p; }
  81.  
  82.         
  83. public:
  84.         p_heap()        { item_count=0; }
  85.  
  86.         p_heap(int)     { error_handler(1,"no p_heap(int) constructor");}
  87.         p_heap(int,int) { error_handler(1,"no p_heap(int,int) constructor");}
  88.  
  89.         p_heap(const p_heap&);
  90.         p_heap& operator=(const p_heap&);
  91.  
  92. virtual ~p_heap() { clear(); }
  93.  
  94.  
  95.         ph_item* insert(GenPtr,GenPtr);
  96.  
  97.         ph_item* find_min() const { return head; }
  98.  
  99.         void decrease_key(ph_item*,GenPtr);
  100.         void delete_min_multipass();
  101.         void delete_min_twopass();
  102.  
  103.         void del_min() { delete_min_twopass(); }
  104.  
  105.         GenPtr key(ph_item* x) const {return x->key;}
  106.         GenPtr inf(ph_item* x) const {return x->inf;}
  107.  
  108.  
  109.         void change_inf(ph_item* x,GenPtr inf)
  110.                 {clear_inf(x->inf);copy_inf(inf);x->inf=inf;}
  111.         void del_item(ph_item* x)
  112.                 {decrease_key(x,head->key);delete_min_twopass();}
  113.  
  114.         void clear ();
  115.         
  116.         int  size() const  { return item_count; }
  117.         int  empty() const { return item_count>0; }
  118.  
  119.         // iteration (not yet implemented)
  120.  
  121.         ph_item* first_item()        const { return 0; }
  122.         ph_item* next_item(ph_item*) const { return 0; }
  123.  
  124. };      
  125.  
  126. #endif
  127.